package com.blue.code.ftl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.blue.code.bean.EntityBean;
import com.blue.core.util.ReflectionUtil;

import freemarker.template.Configuration;
import freemarker.template.Template;

@Component
public class ConvertorHandler implements Handler
{
	private static Logger logger = LoggerFactory.getLogger(ConvertorHandler.class);
	
	@Value("${ftl.convertor}")
	private String convertor;
	@Value("${ftl.model}")
	private String model;
	
	private Template template;
	
	public ConvertorHandler()
	{
	}

	@Override
	public void handle(EntityBean bean, String output, String pkg) throws Exception
	{
		Map<String, Object> root = new HashMap<>();
		root.put("pkg", pkg + "." + "util");
		root.put("modelPkg", pkg + "." + model);
		root.put("messagePkg", pkg + "." + "message");
		root.put("clazz", bean.getClazz());
		root.put("name", bean.getName());
		root.put("field", ReflectionUtil.clazzToField(bean.getClazz()));
		root.put("idList", bean.getIdList());
		root.put("columnList", bean.getColumnList());
		
		String clazz = bean.getClazz() + "Convertor";
		String fileName = this.output(template, root, output, "util", clazz);
		logger.info("输出文件：{}", fileName);
	}

	@Override
	public void initConfiguration(Configuration conf, String ext) throws Exception
	{
		String ftl = convertor + ext;
		template = conf.getTemplate(ftl);
		logger.info("初始化 Convertor 模板：{}", ftl);
	}
}
